﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using System.Collections.ObjectModel;
using System.Linq;
using CommunitySystem.PageView.晋升管理.新的晋升管理;
using EHR.Common;
using CommunitySystem.PageView;
using CommunitySystem.EmpbasicService;
using CommunitySystem.JinShengService;

namespace CommunitySystem
{
    public partial class JinShen_DangGongWeiControl : AsyncCtrl, IReturnListenerc
	{
        private JinShengService.JinShen_MainInfo _MainInfo = null;
        private JinShengService.JinShengServiceClient _Client = null;
        private ObservableCollection<JinShengService.JinShen_DangGongWei_Detail> details = new ObservableCollection<JinShengService.JinShen_DangGongWei_Detail>();
      
        private bool CanEditInfo = true;
        private string Role = "";
        public JinShen_DangGongWeiControl(JinShengService.JinShen_MainInfo mainInfo)
		{
			// 为初始化变量所必需
			InitializeComponent();
            IsWorking.Visibility = Visibility.Visible;
            _MainInfo = mainInfo;
          
            TextBlock tb = new TextBlock() { Text = "科室:" + mainInfo.Department, Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
            selectionInfo.Children.Add(tb);
            tb = new TextBlock() { Text = "职位:" + mainInfo.Position + "(" + mainInfo.PositionLevel + ")", Margin = new Thickness(60, 0, 0, 0), VerticalAlignment = System.Windows.VerticalAlignment.Center };
            selectionInfo.Children.Add(tb);
            _Client = new JinShengService.JinShengServiceClient();

            #region
            _Client.GetOrgForHRCompleted += (sender, e) =>
            {
                Role = e.Result;
                 if (CommunitySystem.PageView.JinShenConst.Administrator.Contains(e.Result))
                {
                    if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Party_Meeting)  //上党工委会
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }
                else
                {
                    if (_MainInfo.Status == JinShenConst.STATUS_Candidate_Inspect || _MainInfo.Status == JinShenConst.STATUS_Party_Meeting)
                    {
                        btnSubmit.IsEnabled = true;
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }

            };
            _Client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);
            #endregion

            _Client.GetJinShen_DangGongWei_DetailCompleted += (sender, e) =>
            {
                details = e.Result;
                if (details != null && details.Count > 0)//编辑
                {
                    if (JinShenConst.Administrator.Contains(Role) && _MainInfo.Status == JinShenConst.STATUS_Party_Meeting)
                        imgBtn2.IsEnabled = false;
                    else
                        imgBtn2.IsEnabled = true;
                       
                    Init();
                 
                }
                else//新增
                {
                    _Client.GetJinShen_KaoCha_DetailCompleted += (sender1, e1) =>
                    {
                     
                        foreach (var d in e1.Result)
                        {
                            JinShengService.JinShen_DangGongWei_Detail detail = new JinShengService.JinShen_DangGongWei_Detail()
                            {
                                EmpID = d.EmpID,
                                EmpName = d.EmpName,
                                MainID = d.MainID,
                            };
                            details.Add(detail);
                        }
                        Init();
                    };
                    _Client.GetJinShen_KaoCha_DetailAsync("MainID=" + _MainInfo.ID);
                }
            };
            _Client.GetJinShen_DangGongWei_DetailAsync("MainID=" + _MainInfo.ID);

            _Client.SaveJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _Client.SaveJinShen_DangGongWei_DetailAsync(details);
                }
            };
            _Client.UpdateJinShenMStatusCompleted += (sender, e) =>
            {


            };
            //消息
            _Client.AddSystemInfoCompleted += (ss, ee) =>
            {

            };
            _Client.SaveJinShen_DangGongWei_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _Client.UpdateJinShenMStatusAsync(_MainInfo.MId, _MainInfo.Status);
                    SilverlightMessageBoxLibrary.Message.InfoMessage("会议决定上报成功");
                    imgBtn2.IsEnabled = true;

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "公示";
                    sysinfo.DW1 = _MainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 2;
                    sysinfo.OrgIDs = _MainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _Client.AddSystemInfoAsync(sysinfo);
                }
            };
           
		}
        private ObservableCollection<JinShengService.JinShen_MainInfo> _mainInfos = null;
        private ObservableCollection<JinShengService.JinShen_MainInfo> SavemainInfos = null;
        public JinShen_DangGongWeiControl(ObservableCollection<JinShengService.JinShen_MainInfo> mainInfos)
        {
            // 为初始化变量所必需
            InitializeComponent();
            gggg.Visibility = Visibility.Collapsed;

            IsWorking.Visibility = Visibility.Visible;

            _mainInfos = mainInfos;
            imgBtn2.IsEnabled = false;
            _Client = new JinShengService.JinShengServiceClient();
            #region
            _Client.GetOrgForHRCompleted += (sender, e) =>
            {
                Role = e.Result;
                if (CommunitySystem.PageView.JinShenConst.Administrator.Contains(e.Result))
                {
                    if (mainInfos.First().Status == CommunitySystem.PageView.JinShenConst.STATUS_Party_Meeting)  //上党工委会
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }
                else
                {
                    if (mainInfos.First().Status == JinShenConst.STATUS_Candidate_Inspect || mainInfos.First().Status == JinShenConst.STATUS_Party_Meeting)
                    {
                        btnSubmit.IsEnabled = true;
                        if (imgBtn2.IsEnabled == false)
                        imgBtn2.IsEnabled = false;
                    }
                    else
                    {
                        btnSubmit.IsEnabled = false;
                        imgBtn2.IsEnabled = true;
                    }
                }

            };
            _Client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);
            #endregion
             JSControl3 js3 = null;
           
        
             foreach (var mainInfo in mainInfos)
             {
                 _MainInfo = mainInfo;
                 if (mainInfo.DangGongWei_Detail.Count != 0)
                 {
                     foreach (var item in mainInfo.DangGongWei_Detail)
                     {
                         details.Add(item);
                     }
                     if (JinShenConst.Administrator.Contains(Role) && _MainInfo.Status == JinShenConst.STATUS_Party_Meeting)
                         imgBtn2.IsEnabled = false;
                     else
                         imgBtn2.IsEnabled = true;
                 }
                 else
                 {
                   
                     foreach (var d in mainInfo.KaoCha_Detail)
                     {
                         JinShengService.JinShen_DangGongWei_Detail detail = new JinShengService.JinShen_DangGongWei_Detail()
                         {
                             EmpID = d.EmpID,
                             EmpName = d.EmpName,
                             MainID = d.MainID,
                         };
                         details.Add(detail);
                     }
                 }

                 js3 = new JSControl3(mainInfo, details);
                 spDetails1.Children.Add(js3);
             }
             IsWorking.Visibility = Visibility.Collapsed;

             _Client.SaveJinShen_DangGongWei_DetailCompleted += (sender, e) =>
             {
                 if (e.Error == null)
                 {
                    // SilverlightMessageBoxLibrary.Message.InfoMessage("党(工)委(组)考察成功!");
                     var Status = CommunitySystem.PageView.JinShenConst.STATUS_Party_Meeting;
                     _Client.SaveJinShenMainInfosStatusAsync(_mainInfos, Status, _MainInfo.MId, EHR.Common.Session.LoginUser.LoginName);
             
                     imgBtn2.IsEnabled = true;
                 }
             };
             //消息
             _Client.AddSystemInfoCompleted += (ss, ee) =>
             {

             };
            _Client.SaveJinShenMainInfosStatusCompleted += (sender, e) => {

                if (e.Error == null)
                {
                    SavemainInfos = e.Result[0].JinShen_MainInfos;
                    SilverlightMessageBoxLibrary.Message.InfoMessage("会议决定上报成功");
                  //  _Client.SaveJinShen_DangGongWei_DetailAsync(details);

                    //添加消息
                    SystemInfo sysinfo = new SystemInfo();
                    sysinfo.MenuId = 376;
                    sysinfo.MoKuai = "晋升管理";
                    sysinfo.MoKuaiSX = "公示";
                    sysinfo.DW1 = _MainInfo.OrgName;
                    sysinfo.CreateUser = Session.LoginUser.RealName;
                    sysinfo.CreateDate = DateTime.Now.ToString();
                    sysinfo.CreateUserID = 2;
                    sysinfo.OrgIDs = _MainInfo.OrgID.ToString();
                    sysinfo.LogUID = Session.LoginUser.UserID;
                    _Client.AddSystemInfoAsync(sysinfo);
                }
            };

         

        }

        public void BeginReturn()
        {
            return;
        }
        private ObservableCollection<BianZhiModel> ks = new ObservableCollection<BianZhiModel>();
        private void Init()
        {
            if (_MainInfo.OrgID > 0 && string.IsNullOrEmpty(_MainInfo.Department))
            {
                InitKS();
            }
            else
            {

                foreach (var d in details)
                {
                    Grid grid = new Grid() { Margin = new Thickness(3) };
                    Border b = new Border() { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.Blue), HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    Grid.SetRow(b, 0);
                    Grid.SetRowSpan(b, 4);
                    grid.Children.Add(b);
                    grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                    grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                    grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                    grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3), DataContext = d, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch };
                    Grid.SetRow(sp, 0);
                    TextBlock tb = new TextBlock() { Text = "经党（工）委（组）", Margin = new Thickness(3) };
                    sp.Children.Add(tb);
                    DatePicker dp = new DatePicker();
                    //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree || CanEditInfo)
                    dp.IsEnabled = CanEditInfo;
                    dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("MeetingDate")
                    {
                        Mode = System.Windows.Data.BindingMode.TwoWay,
                        StringFormat = "yyyy-MM-dd"
                    });
                    sp.Children.Add(dp);
                    tb = new TextBlock() { Text = "日会议讨论决定如下：", Margin = new Thickness(3) };
                    sp.Children.Add(tb);
                    grid.Children.Add(sp);
                    sp = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                    tb = new TextBlock() { Text = d.EmpName, Margin = new Thickness(5, 6, 0, 3) };
                    DictComboBox comb = new DictComboBox() { DictCode = "IsAgree", Field = "IsAgree", DataContext = d, SelectedValuePath = "DictValue", DisplayMemberPath = "DictValue", Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0) };
                    //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                    comb.IsEnabled = CanEditInfo;
                    comb.SetBinding(DictComboBox.SelectedValueProperty, new System.Windows.Data.Binding("IsAgree") { Mode = System.Windows.Data.BindingMode.TwoWay });
                    //TextBlock tb2 = new TextBlock() { Text = "同志拟任" + _MainInfo.Department + _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };


                    TextBlock tb2 = new TextBlock() { Text = "同志拟任" + _MainInfo.Department + _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };



                    sp.Children.Add(comb);
                    sp.Children.Add(tb);

                    sp.Children.Add(tb2);

                    //<local:DictTextBox  IsEnabled="False" x:Name="dtbPosition" Grid.Row="1"  Grid.Column="1"  Margin="8,4,8,2" HorizontalAlignment="Stretch" DictCode="zwdm" DictViewType="Name"  DictResultType="Name"  Field="Position"   VerticalAlignment="Stretch" />

                    tb2 = new TextBlock() { Text = "职位:", Margin = new Thickness(10, 6, 3, 3) };
                    DictTextBox dtb1 = new DictTextBox() { Field = "Position", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwdm", Margin = new Thickness(3), Width = 120, DataContext = d };
                    sp.Children.Add(tb2);
                    sp.Children.Add(dtb1);



                    tb2 = new TextBlock() { Text = "职务层次:", Margin = new Thickness(10, 6, 3, 3) };
                    DictTextBox dtb = new DictTextBox() { Field = "AttributLevel", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwcc", Margin = new Thickness(3), Width = 120, DataContext = d };
                    //tb = new TextBlock() { Text = "晋升为" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(3) };
                    dtb.IsEnabled = CanEditInfo;
                    sp.Children.Add(tb2);
                    sp.Children.Add(dtb);

                    //<local:DictTextBox x:Name="dtbPositionAttr" Width="120" Margin="5,8"  HorizontalAlignment="Stretch" DictCode="zwsx" DictViewType="Name"  DictResultType="Name"  Field="PositionAttibute"   VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>

                    tb2 = new TextBlock() { Text = "职位属性:", Margin = new Thickness(10, 6, 3, 3) };
                    DictTextBox dtb2 = new DictTextBox() { Field = "PositionAttibute", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwsx", Margin = new Thickness(3), Width = 120, DataContext = d };
                    sp.Children.Add(tb2);
                    sp.Children.Add(dtb2);


                    grid.Children.Add(sp);



                    StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                    tb2 = new TextBlock() { Text = "任期（年）:", Margin = new Thickness(3, 6, 3, 3) };
                    sp2.Children.Add(tb2);

                    TextBox textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                    textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("RenQi") { Mode = System.Windows.Data.BindingMode.TwoWay });
                    textBox.IsEnabled = CanEditInfo;
                    sp2.Children.Add(textBox);

                    tb2 = new TextBlock() { Text = "是否有试用期:", Margin = new Thickness(10, 3, 0, 3) };
                    DictComboBox dcb = new DictComboBox() { Field = "HasShiYongQi", DisplayMemberPath = "DictValue", SelectedValuePath = "DictValue", DictCode = "sf", Margin = new Thickness(3), Width = 50, Height = 23, DataContext = d };
                    dcb.IsEnabled = CanEditInfo;
                    dcb.SelectionChanged += new SelectionChangedEventHandler(dcb_SelectionChanged);
                    StackPanel shiyongqi = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Center, Visibility = System.Windows.Visibility.Collapsed };
                    dcb.Tag = shiyongqi;
                    textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                    textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("ShiYongQiYear") { Mode = System.Windows.Data.BindingMode.TwoWay });
                    textBox.IsEnabled = CanEditInfo;
                    TextBlock tb3 = new TextBlock() { Text = "试用期(年):", Margin = new Thickness(10, 3, 0, 3) };
                    shiyongqi.Children.Add(tb3);
                    shiyongqi.Children.Add(textBox);
                    sp2.Children.Add(tb2);

                    sp2.Children.Add(dcb);
                    sp2.Children.Add(shiyongqi);

                    ////grid.Children.Add(sp2);  //任期
                    ////Grid.SetRow(sp2, 2);

                    Grid.SetRow(sp, 1);

                    tb = new TextBlock() { Text = "特此证明", HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Margin = new Thickness(3, 3, 100, 0) };
                    Grid.SetRow(tb, 3);
                    grid.Children.Add(tb);
                    spDetails.Children.Add(grid);
                }
                IsWorking.Visibility = Visibility.Collapsed;
            }
           
        }
        private void InitKS()
        {
            if (_MainInfo.OrgID > 0 && string.IsNullOrEmpty(_MainInfo.Department))
            {
                EmpBasicServiceClient empclient = new EmpBasicServiceClient();
                empclient.GetBianZhiModelCompleted += (s, e) =>
                {
                    ks = e.Result;

                    foreach (var d in details)
                    {
                        Grid grid = new Grid() { Margin = new Thickness(3) };
                        Border b = new Border() { BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.Blue), HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                        Grid.SetRow(b, 0);
                        Grid.SetRowSpan(b, 4);
                        grid.Children.Add(b);
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(33, GridUnitType.Star) });
                        StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3), DataContext = d, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch };
                        Grid.SetRow(sp, 0);
                        TextBlock tb = new TextBlock() { Text = "经党（工）委（组）", Margin = new Thickness(3) };
                        sp.Children.Add(tb);
                        DatePicker dp = new DatePicker();
                        //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree || CanEditInfo)
                        dp.IsEnabled = CanEditInfo;
                        dp.SetBinding(DatePicker.SelectedDateProperty, new System.Windows.Data.Binding("MeetingDate")
                        {
                            Mode = System.Windows.Data.BindingMode.TwoWay,
                            StringFormat = "yyyy-MM-dd"
                        });
                        sp.Children.Add(dp);
                        tb = new TextBlock() { Text = "日会议讨论决定如下：", Margin = new Thickness(3) };
                        sp.Children.Add(tb);
                        grid.Children.Add(sp);
                        sp = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                        tb = new TextBlock() { Text = d.EmpName, Margin = new Thickness(5, 6, 0, 3) };
                        DictComboBox comb = new DictComboBox() { DictCode = "IsAgree", Field = "IsAgree", DataContext = d, SelectedValuePath = "DictValue", DisplayMemberPath = "DictValue", Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0) };
                        //if (_MainInfo.Status == CommunitySystem.PageView.JinShenConst.STATUS_Promot_Agree)
                        comb.IsEnabled = CanEditInfo;
                        comb.SetBinding(DictComboBox.SelectedValueProperty, new System.Windows.Data.Binding("IsAgree") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        //TextBlock tb2 = new TextBlock() { Text = "同志拟任" + _MainInfo.Department + _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };

                        TextBlock tb2 = null;
                        TextBlock tb2s = null;
                        ComboBox cbks = null;

                        tb2s = new TextBlock() { Text = "同志拟任", Margin = new Thickness(0, 6, 3, 0) };
                        cbks = new ComboBox() { DisplayMemberPath = "JiGouMingCheng", Height = 25, Width = 100 };
                        cbks.SelectionChanged += (sender, ee) => {
                            if (((ComboBox)sender).SelectedItem != null)
                            {
                                _MainInfo.Department = ((BianZhiModel)((((ComboBox)sender).SelectedItem))).JiGouMingCheng;
                            }
                        
                        };
                        cbks.ItemsSource = ks;
                        tb2 = new TextBlock() { Text = _MainInfo.Position, Margin = new Thickness(0, 6, 3, 0) };


                        sp.Children.Add(comb);
                        sp.Children.Add(tb);


                        sp.Children.Add(tb2s);
                        sp.Children.Add(cbks);

                        sp.Children.Add(tb2);

                        //<local:DictTextBox  IsEnabled="False" x:Name="dtbPosition" Grid.Row="1"  Grid.Column="1"  Margin="8,4,8,2" HorizontalAlignment="Stretch" DictCode="zwdm" DictViewType="Name"  DictResultType="Name"  Field="Position"   VerticalAlignment="Stretch" />

                        tb2 = new TextBlock() { Text = "职位:", Margin = new Thickness(10, 6, 3, 3) };
                        DictTextBox dtb1 = new DictTextBox() { Field = "Position", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwdm", Margin = new Thickness(3), Width = 120, DataContext = d };
                        sp.Children.Add(tb2);
                        sp.Children.Add(dtb1);



                        tb2 = new TextBlock() { Text = "职务层次:", Margin = new Thickness(10, 6, 3, 3) };
                        DictTextBox dtb = new DictTextBox() { Field = "AttributLevel", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwcc", Margin = new Thickness(3), Width = 120, DataContext = d };
                        //tb = new TextBlock() { Text = "晋升为" + _MainInfo.Position + "(" + _MainInfo.PositionLevel + ")", Margin = new Thickness(3) };
                        dtb.IsEnabled = CanEditInfo;
                        sp.Children.Add(tb2);
                        sp.Children.Add(dtb);

                        //<local:DictTextBox x:Name="dtbPositionAttr" Width="120" Margin="5,8"  HorizontalAlignment="Stretch" DictCode="zwsx" DictViewType="Name"  DictResultType="Name"  Field="PositionAttibute"   VerticalAlignment="Stretch" d:LayoutOverrides="Height"/>

                        tb2 = new TextBlock() { Text = "职位属性:", Margin = new Thickness(10, 6, 3, 3) };
                        DictTextBox dtb2 = new DictTextBox() { Field = "PositionAttibute", DictViewType = DictViewType.Name, DictResultType = DictResultType.Name, DictCode = "zwsx", Margin = new Thickness(3), Width = 120, DataContext = d };
                        sp.Children.Add(tb2);
                        sp.Children.Add(dtb2);


                        grid.Children.Add(sp);



                        StackPanel sp2 = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Margin = new Thickness(120, 3, 3, 3) };
                        tb2 = new TextBlock() { Text = "任期（年）:", Margin = new Thickness(3, 6, 3, 3) };
                        sp2.Children.Add(tb2);

                        TextBox textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                        textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("RenQi") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        textBox.IsEnabled = CanEditInfo;
                        sp2.Children.Add(textBox);

                        tb2 = new TextBlock() { Text = "是否有试用期:", Margin = new Thickness(10, 3, 0, 3) };
                        DictComboBox dcb = new DictComboBox() { Field = "HasShiYongQi", DisplayMemberPath = "DictValue", SelectedValuePath = "DictValue", DictCode = "sf", Margin = new Thickness(3), Width = 50, Height = 23, DataContext = d };
                        dcb.IsEnabled = CanEditInfo;
                        dcb.SelectionChanged += new SelectionChangedEventHandler(dcb_SelectionChanged);
                        StackPanel shiyongqi = new StackPanel() { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Center, Visibility = System.Windows.Visibility.Collapsed };
                        dcb.Tag = shiyongqi;
                        textBox = new TextBox() { Width = 80, Height = 23, Margin = new Thickness(5, 0, 0, 0), DataContext = d };
                        textBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("ShiYongQiYear") { Mode = System.Windows.Data.BindingMode.TwoWay });
                        textBox.IsEnabled = CanEditInfo;
                        TextBlock tb3 = new TextBlock() { Text = "试用期(年):", Margin = new Thickness(10, 3, 0, 3) };
                        shiyongqi.Children.Add(tb3);
                        shiyongqi.Children.Add(textBox);
                        sp2.Children.Add(tb2);

                        sp2.Children.Add(dcb);
                        sp2.Children.Add(shiyongqi);

                        ////grid.Children.Add(sp2);  //任期
                        ////Grid.SetRow(sp2, 2);

                        Grid.SetRow(sp, 1);

                        tb = new TextBlock() { Text = "特此证明", HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Margin = new Thickness(3, 3, 100, 0) };
                        Grid.SetRow(tb, 3);
                        grid.Children.Add(tb);
                        spDetails.Children.Add(grid);
                    }
                };
                empclient.GetBianZhiModelAsync(_MainInfo.OrgID.ToString(), 0);
            }

          
            IsWorking.Visibility = Visibility.Collapsed;

        }

        void dcb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var dcb = sender as DictComboBox;
            var detail = dcb.DataContext as JinShengService.JinShen_DangGongWei_Detail;
            var sp = dcb.Tag as StackPanel;
            if (dcb.SelectedValue != null && dcb.SelectedValue.Equals("是"))
            {
                sp.Visibility = System.Windows.Visibility.Visible;
            }
            else if (dcb.SelectedValue != null && dcb.SelectedValue.Equals("否"))
            {
                sp.Visibility = System.Windows.Visibility.Collapsed;
                detail.ShiYongQiYear = 0;
            }
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                if (_mainInfos == null)
                {
                    if (string.IsNullOrEmpty(_MainInfo.Department))
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage("请选择科室名称！");
                        return;
                    }

                    _MainInfo.Status = CommunitySystem.PageView.JinShenConst.STATUS_Party_Meeting;
                    _Client.SaveJinShenMainInfoAsync(_MainInfo);
                }
                else
                {
                    var ksnull = _mainInfos.Where(p => p.Department==null).ToList();
                    if (ksnull != null && ksnull.Count>0)
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage("请选择科室名称！");
                        return;
                    }
                    _Client.SaveJinShen_DangGongWei_DetailAsync(details);
                }
            }
        }

        private bool Validate()
        {
            var detail = details.Where(c => c.MeetingDate == null).FirstOrDefault();
            if (detail != null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("针对" + detail.EmpName + "同志讨论会议日期不能为空!");
                return false;
            }

            detail = details.Where(c => string.IsNullOrEmpty(c.IsAgree)).FirstOrDefault();
            if (detail != null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请填写" + detail.EmpName + "同志讨论会议决定!");
                return false;
            }
            if (_mainInfos == null)
            {
                int count = details.Where(c => c.IsAgree == "同意").Count();
                if (count >= 2)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("最多只能有一位通知进入公示阶段!");
                    return false;
                }
            }
            else
            {
                foreach (var item in _mainInfos)
                {
                    int count = details.Where(p=>p.MainID==item.ID).ToList().Where(c => c.IsAgree == "同意").Count();
                    var aa=details.Where(p=>p.MainID==item.ID).ToList().Where(c => c.IsAgree == "同意").ToList().Count!=0?details.Where(p=>p.MainID==item.ID).ToList().Where(c => c.IsAgree == "同意").ToList().First():null;
                    if (count >= 2)
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage(item.Department+"科最多只能有一位通知进入公示阶段!");
                        return false;
                    }
                    foreach (var item1 in details.Where(p=>p.MainID!=item.ID).ToList().Where(c => c.IsAgree == "同意").ToList())
                    {
                        if(aa!=null)
                        if (aa.EmpID == item1.EmpID)
                        {
                            SilverlightMessageBoxLibrary.Message.InfoMessage("同一人员不能多岗位晋升！");
                            return false;
                        }
                    }
                }
               
            }
            return true;
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void imgBtn2_Click(object sender, RoutedEventArgs e)
        {
             if (_mainInfos == null)
            {
             
            CommunitySystem.PageView.PublicShowControl control = new CommunitySystem.PageView.PublicShowControl(_MainInfo) { Nav = this.Nav };
            this.Nav.ShowNextPage("公示", control as AsyncCtrl);
             }
             if (_mainInfos != null)
            {
                //if (SavemainInfos == null) SavemainInfos = _mainInfos;
                //CommunitySystem.PageView.PublicShowControl control = new CommunitySystem.PageView.PublicShowControl(SavemainInfos) { Nav = this.Nav };
                //this.Nav.ShowNextPage("公示", control as AsyncCtrl);

                _Client.GetJinShen_MainInfosCompleted -= new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
                _Client.GetJinShen_MainInfosCompleted += new EventHandler<JinShengService.GetJinShen_MainInfosCompletedEventArgs>(_client_GetJinShen_MainInfosCompleted);
                _Client.GetJinShen_MainInfosAsync(string.Format("1=1 and jsm.ID={0}", _mainInfos.First().MId), Session.LoginUser.LoginName);
            }
        }
        void _client_GetJinShen_MainInfosCompleted(object sender, JinShengService.GetJinShen_MainInfosCompletedEventArgs e)
        {
            CommunitySystem.PageView.PublicShowControl control = new CommunitySystem.PageView.PublicShowControl(e.Result[0].JinShen_MainInfos) { Nav = this.Nav };
            this.Nav.ShowNextPage("公示", control as AsyncCtrl);
        }
    }
}